package cn.gson.crm.controller;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 公共servlet
 */

public class BaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private HttpServletResponse response;

	protected void forward(String page) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/jsp/" + page).forward(request, response);
	}

	protected void erroForward(String page, String message) throws ServletException, IOException {
		request.setAttribute("erro", message);
		forward(page);
	}

	protected void redirect(String path) throws IOException {
		String basePath = (String) request.getAttribute("basePath");
		response.sendRedirect(basePath + path);
	}

	protected void showErro(Exception e) throws ServletException, IOException {
		showErro(e.getMessage());
	}

	protected void showErro(String message) throws ServletException, IOException {
		request.setAttribute("message", message);
		forward("erro.jsp");
	}

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		setResponseAndResquest(request, response);
		String method = request.getMethod();

		try {
			if (method.equalsIgnoreCase("get")) {
				get(request, response);
			} else if (method.equalsIgnoreCase("post")) {
				post(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
			showErro(e);
		}

	}

	protected void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.getWriter().append("请重写get请求");
	}

	protected void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.getWriter().append("请重写post请求");
	}

	private void setResponseAndResquest(HttpServletRequest request, HttpServletResponse response) {
		this.request = request;
		this.response = response;
	}

	/**
	 * 分页字符串
	 * 
	 * @param request
	 * @return
	 */
	protected String noPageQueryString(HttpServletRequest request) {
		Map<String, String[]> params = request.getParameterMap();
		String paramsStr = "";
		int i = 0;
		for (String key : params.keySet()) {
			if ("page".equals(key)) {
				continue;
			}
			String[] valus = params.get(key);
			String query = "";
			for (int j = 0; j < valus.length; j++) {
				query += key + "=" + valus[j];
				if (j < valus.length - 1) {
					query += "&";
				}
			}
			paramsStr += query;
			if (i < params.keySet().size() - 1) {
				paramsStr += "&";
			}
			i++;
		}
		return paramsStr.isEmpty() ? "" : "&" + paramsStr;
	}

	/**
	 * 删除 Session
	 * 
	 * @param request
	 * @param sessionString
	 */
	protected void setReqAndRemoveRes(String sessionStr, HttpServletRequest request, String sessionString) {
		Object cantBeDelDeptList = request.getSession().getAttribute(sessionString);
		request.setAttribute(sessionStr, cantBeDelDeptList);
		request.getSession().removeAttribute(sessionString);
	}
	
	//截取链接最后url
	protected void getServletPathAndForward(HttpServletRequest request, String jspUrl)
			throws ServletException, IOException {
		String url = request.getServletPath();
		request.setAttribute("url", url.substring(1));
		forward(jspUrl);
	}
	
	protected void getServletPathAndForward1(HttpServletRequest request, String jspUrl)
			throws ServletException, IOException {
		String url = request.getServletPath();
		String[] array = null;
		if(url.indexOf("/")!=-1){
			array = url.substring(1).split("/");
			System.out.println(array[0]);
		}
		request.setAttribute("url", array[0]);
		forward(jspUrl);
	}
	
	protected  String getCurrentDayZero() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar calendar1 = Calendar.getInstance();
		calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
				0, 0, 0);
		Date beginOfDate = calendar1.getTime();
		return sdf.format(beginOfDate);
	}
	
}
